package cn.tarena.ht.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tarena.ht.pojo.Role;
import cn.tarena.ht.pojo.User;
import cn.tarena.ht.service.RoleService;
import cn.tarena.ht.service.UserService;
import cn.tarena.ht.tool.IdCheck;
import cn.tarena.ht.tool.Md5HashTool;

@Controller
@RequestMapping("/sysadmin/user")
public class UserController extends BaseController{
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/list")
	public String findAll(Model model) {
		List<User> userList = userService.findAll();
		model.addAttribute("userList", userList);
		return "/sysadmin/user/jUserList";
	}
	@RequestMapping("/start")
	public String toStart(@RequestParam(value = "userId", required=false)String[] userIds, HttpSession session) {
		int state = 1;
		boolean result = IdCheck.checkIds(userIds, session);
		if(result) {
			userService.updateState(userIds,state);
		}
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/stop")
	public String toStop(@RequestParam(value = "userId", required=false)String[] userIds, HttpSession session) {
		int state = 0;
		boolean result = IdCheck.checkIds(userIds, session);
		if(result) {
			userService.updateState(userIds,state);
		}
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/delete")
	public String toDelete(@RequestParam(value = "userId", required=false)String[] userIds, HttpSession session) {
		boolean result = IdCheck.checkIds(userIds, session);
		if(result) {
			userService.deleteById(userIds);
		}
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/tocreate")
	public String toCreate(Model model) {
		List<User> managerList = userService.findAll();
		model.addAttribute("managerList", managerList);
		return "/sysadmin/user/jUserCreate";
	}
	@RequestMapping("/save")
	public String saveUser(User user) {
		user.setPassword(Md5HashTool.getPassword(user.getUsername(), user.getPassword()));
		user.setCreateTime(new Date());
		user.setUpdateTime(user.getCreateTime());
		user.getUserInfo().setCreateTime(user.getCreateTime());
		user.getUserInfo().setUpdateTime(user.getCreateTime());
		userService.saveUser(user);
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/toview")
	public String toView(@RequestParam(value="userId", required=false)String[] userIds,HttpSession session) {
		boolean result = IdCheck.checkId(userIds, session);
		if(result) {
			User user = userService.findUserById(userIds[0]);
			session.setAttribute("user", user);
			return "/sysadmin/user/jUserView";
		}
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/toupdate")
	public String toUpdate(@RequestParam(value="userId", required=false)String[] userIds,HttpSession session) {
		boolean result = IdCheck.checkId(userIds, session);
		if(result) {
			User user = userService.findUserById(userIds[0]);
			List<User> managerList = userService.findAll();
			session.setAttribute("user", user);
			session.setAttribute("managerList", managerList);
			return "/sysadmin/user/jUserUpdate";
		}
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/update")
	public String UpdateUser(User user) {
		user.setPassword(Md5HashTool.getPassword(user.getUsername(), user.getPassword()));
		user.setUpdateTime(new Date());
		userService.updateUser(user);
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/userRole")
	public String userRole(@RequestParam(value="userId", required=false)String[] userIds,HttpSession session) throws JsonProcessingException {
		boolean result = IdCheck.checkId(userIds, session);
		if(result) {
			List<String> uRoleList = userService.findUserRoleIdList(userIds[0]);
			List<Role> roleList = roleService.findAll();
			for (Role role : roleList) {
				if(uRoleList.contains(role.getRoleId())) {
					role.setChecked(true);
				}
			}
			ObjectMapper objectMapper = new ObjectMapper();
			String zTreeJson = objectMapper.writeValueAsString(roleList);
			session.setAttribute("zTreeJson", zTreeJson);
			session.setAttribute("userId", userIds[0]);
			return "/sysadmin/user/jUserRolezTree";
		}else {
			return "redirect:/sysadmin/user/list";
		}
	}
	@RequestMapping("/saveUserRole")
	public String saveUserRole(String userId,String[] roleIds,HttpSession session) {
		userService.saveUserRole(userId,roleIds);
		return "redirect:/sysadmin/user/list";
	}
}
